﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BHXH.Repository;
namespace Lists
{
    public class PhieuB : DoiTuong
    {
        #region LoaiPhieu property
        public virtual string LoaiPhieu
        {
            get
            {
                return "PhieuB";
            }
        }
        #endregion

        #region MaPhieu property
        public string MaPhieu
        {
            get
            {
                return (string)_DataRow["MaPhieu"];
            }
            set
            {
                _DataRow["MaPhieu"] = value;
            }
        }
        #endregion

        #region SoPhieu property
        public string SoPhieu
        {
            get
            {
                return (string)_DataRow["SoPhieu"];
            }
            set
            {
                _DataRow["SoPhieu"] = value;
            }
        }
        #endregion

        #region NgayPV property
        public DateTime NgayPV
        {
            get
            {
                return (DateTime)_DataRow["NgayPV"];
            }
            set
            {
                _DataRow["NgayPV"] = value;
            }
        }
        #endregion

        #region CoQuan property
        public CoQuan CoQuan
        {
            get
            {
                if (_DataRow["CoQuanID"] is Guid)
                    return DanhMucCoQuan.Instance.GetByID((Guid)_DataRow["CoQuanID"]);
                else
                    return null;
            }
            set
            {
                if (value is CoQuan)
                    _DataRow["CoQuanID"] = value.ID;
                else
                    _DataRow["CoQuanID"] = DBNull.Value;
            }
        }
        #endregion

        #region NVDieuTra property
        public NhanVienDieuTraGiamSat NVDieuTra
        {
            get
            {
                if (_DataRow["NVDieuTraID"] is Guid)
                    return DanhMucNhanVienDieuTraGiamSat.Instance.GetByID((Guid)_DataRow["NVDieuTraID"]);
                else
                    return null;
            }
            set
            {
                if (value is NhanVienDieuTraGiamSat)
                    _DataRow["NVDieuTraID"] = value.ID;
                else
                    _DataRow["NVDieuTraID"] = DBNull.Value;
            }
        }
        #endregion

        #region NVGiamSat property
        public NhanVienDieuTraGiamSat NVGiamSat
        {
            get
            {
                if (_DataRow["NVGiamSatID"] is Guid)
                    return DanhMucNhanVienDieuTraGiamSat.Instance.GetByID((Guid)_DataRow["NVGiamSatID"]);
                else
                    return null;
            }
            set
            {
                if (value is NhanVienDieuTraGiamSat)
                    _DataRow["NVGiamSatID"] = value.ID;
                else
                    _DataRow["NVGiamSatID"] = DBNull.Value;
            }
        }
        #endregion

        #region NguoiTraLoiPV property
        public NguoiDuocPV NguoiTraLoiPV
        {
            get
            {
                if (_DataRow["NguoiTraLoiPVID"] is Guid)
                    return DanhMucNguoiDuocPV.Instance.GetByID((Guid)_DataRow["NguoiTraLoiPVID"]);
                else
                    return null;
            }
            set
            {
                if (value is NguoiDuocPV)
                    _DataRow["NguoiTraLoiPVID"] = value.ID;
                else
                    _DataRow["NguoiTraLoiPVID"] = DBNull.Value;
            }
        }
        #endregion

        #region GiaMuaHienTai property
        public decimal GiaMuaHienTai
        {
            get
            {
                return (decimal)_DataRow["GiaMuaHienTai"];
            }
            set
            {
                _DataRow["GiaMuaHienTai"] = value;
            }
        }
        #endregion

        #region DoiTuong property
        private decimal _DoiTuong;
        public decimal DoiTuong
        {
            get
            {
                if (_DataRow["DoiTuong"] is decimal)
                    return (decimal)_DataRow["DoiTuong"];
                else
                    return 0;
            }
            set
            {
                _DataRow["DoiTuong"] = value;
            }
        }
        #endregion

        #region GhiChu property
        public string GhiChu
        {
            get
            {
                return (string)_DataRow["GhiChu"];
            }
            set
            {
                _DataRow["GhiChu"] = value;
            }
        }
        #endregion

        public PhieuB(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucPhieuB : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucPhieuB _Instance;
        public static DanhMucPhieuB Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucPhieuB();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Phieu";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new PhieuB((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public PhieuB this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new PhieuB(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public PhieuB NewDoiTuong()
        {
            return new PhieuB(DataTable.NewRow());
        }

        public PhieuB GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new PhieuB(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public PhieuB GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucPhieuB GetAll()
        {
            return Instance;
        }

        public static DanhMucPhieuB Filter(string filterExpression)
        {
            try
            {
                DanhMucPhieuB obj = new DanhMucPhieuB();
                obj.DataView = new DataView(DanhMucPhieuB.Instance.DataTable, filterExpression, "SoPhieu", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static PhieuB GetByMa(string ma)
        {
            try
            {
                return DanhMucPhieuB.Instance.GetByProperty("MaPhieu", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
